#include <iostream>
#include <unordered_map>
#include <fstream>
#include <string>
#include "log.hpp"


const std::string dictname = "./word.txt";
const std::string sep = ":";


class Init
{
private:
    bool Split(std::string line, std::string* part1, std::string* part2)
    {
        auto it = line.find(sep);
        if(it == std::string::npos) return false;
        *part1 = line.substr(0, it);
        *part2 = line.substr(it + 1);

        return true;
    }
public:
    Init()
    {
        std::ifstream in(dictname);
        if(!in.is_open())
        {
            lg(Fatal, "ifstream open %s error", dictname.c_str());
            exit(1);
        }
        std::string line;
        while(std::getline(in, line))
        {
            std::string part1, part2;
            Split(line, &part1, &part2);
            //_dict.insert({part1, part2});
            _dict[part1] = part2;
        }
        in.close();
    }
    std::string Translation(std::string word)
    {
        for(auto& wd : _dict)
        {
            if(wd.first == word)
            {
                return wd.second;
            }
        }
        return "Unknow";
    }
private:
    std::unordered_map<std::string, std::string> _dict;
};